import axios from "axios";
import {HostName} from './hosts'

const service = new axios.create({
    baseURL: HostName,
    timeout: 30000,
    headers: []
});

//网络请求拦截器
service.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token');
        if(token){
            config.headers.token = token;
        }
        return config;
    },
    error=>{
        console.log(error);
    }

);

//返回结果拦截器
service.interceptors.response.use(
    response => {
        const {status,statusText,data} = response;
        if(status != 200){
            localStorage.removeItem('token');
        }

        return data;
    },
    error=>{
        console.log(error);
    }
);

export default service;